package gin.dev.update;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.vstore.util.GetUrlMethod;
import anroid.vstore.provider.Constant;

public class Installation {

	public static boolean Install(Activity context, String path)
	{
		try
		{
			File file = new File(path);	
			Intent intent = new Intent(Intent.ACTION_VIEW);	
			intent.setDataAndType(Uri.fromFile(file),
					"application/vnd.android.package-archive");
			
			context.startActivityForResult(intent,Constant.APP_INSTALLATION);	
			return true;
		}
		catch (Exception ex)
		{
			Log.i("TAG", ex.getMessage());
		}
		return false;
	}
	
	public static boolean AlreadyDownloaded(Context context, String packageName)
	{
		String path = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + "/Download/" + packageName + ".apk";
		File fapk = new File(path);
		if(fapk.exists() == true)
		{
			return true;
		}
		return false;
	}
	
	public static boolean IsInstalled(Context context, String packageName)
	{
		PackageManager pm = context.getPackageManager();
	    boolean installed = false;
	    try {
	    	PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);

	       installed = true;
	    } catch (PackageManager.NameNotFoundException e) {
	       installed = false;
	    }
	    return installed;
	}
	public static PackageInfo StatusApp(Context context, String packageName)
	{
		PackageManager pm = context.getPackageManager();
//	    boolean installed = false;
	    try {
	    	PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_META_DATA);
	    	
	       return pi;
	    } catch (Exception e) {
//	       installed = false;
	    	Log.d("update stt", e.getMessage());
	    }
	    return null;
	}
	
	public static boolean DeleteApk(Context context, String packageName)
	{
		String path = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + "/Download/" + packageName + ".apk";
		File fapk = new File(path);
		if(fapk.exists() == true)
		{
			return fapk.delete();
		}
		return false;
	}
	
	public static boolean OpenApp(Context context, String packageName)
	{
		Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
	    if (intent != null)
	    {
	        // we found the activity
	        // now start the activity
	        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	        context.startActivity(intent);
	        return true;
	    }
		return false;
	}
	
	public static boolean PostInfoInstallSuccess(String deviceID, String productID)
	{
		String urlRegisterApp = "http://123.30.129.248/api/installsuccess.php";
		
		String respond = GetUrlMethod.postRequest(urlRegisterApp +"?deviceID="+deviceID +"&appID="+productID);
		Log.i("TAG", "respond install message : " + respond);
		
		String urlCountApp = "http://123.30.129.248/api/countup.php";
		respond = GetUrlMethod.postRequest(urlCountApp +"?id=" + productID);		
		Log.i("TAG", "respond count app message : " + respond);
		return true;
	}
}
